<?php

session_start();

//header("Content-Type:application/vnd.adobe.pdf");
//header("Content-Type:application/vnd.adobe.pdf");
$fullname = $_SESSION["fullname"];
include_once ("fpdf/TCPDF/tcpdf.php");
//include_once ("fpdf/");


class PDF extends TCPDF {

    function setVar($fullname) {
        $this->fullname = $fullname;
    }

    function Header() {
        $this->SetFont("thsarabun", "B", 14);
        $header = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><thead>
                <tr>
                  <td height="33" colspan="5">เทศบาลนครอุดรธานี (ผู้ปฏิบัติงาน : ' . $this->fullname . ')</td>
                  <td colspan="4" align="right">วันที่ ' . date('d / m / Y') . '</td>
                </tr>
                </thead>
                </table>
                <table width="100%" border="1" cellspacing="0" cellpadding="0"><thead>
                <tr align="center" nobr="true" valign="middle" >
                <td rowspan="2" width="10%" >เลขที่เช็ค</td>
                <td rowspan="2" width="7%" >เลขที่ฎีกา</td>
                <td rowspan="2" width="20%">ผู้รับเงิน</td>
                <td rowspan="1" colspan="3" width="20%">รหัส</td>
                <td rowspan="1" colspan="2" width="30%">จำนวนเงิน</td>
                <td rowspan="2" width="13%">ลายมือชื่อผู้รับเงิน</td>
                </tr>
                 <tr align="center" nobr="true" style="border-bottom: 1px dotted black">
                <td width="10%">งาน</td>
                <td width="5%">หมวด</td>
                <td width="5%">ประเภท</td>
                <td width="15%" border="0">หน้าฎีกา</td>
                <td width="15%" border="0">ตามเช็ค</td>
            </tr>
               </thead></table><hr>';
        $this->writeHTML('<br><div align="center">รายงานจัดทำเช็ค</div>' . $header, true, false, false, false, '');
    }
    
    function Footer() {}

}
//echo "adsfads";

//$pdf = new TCPDF($orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);
$pdf = new PDF($orientation = 'L', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);

$pdf->setVar($fullname);
$pdf->SetMargins(10, 38, 10);
$pdf->SetAutoPageBreak(true, 20);
$pdf->AddPage();

//$pdf->SetFont("thsarabunb", "", 14);
$pdf->SetFont('thsarabun', '', 14);

require_once("includes/connect.php");
require_once("includes/function.php");

$db = new myDBS();
$db->selectDB("account");

$sql = "SELECT DISTINCT bank.bank_id, bank_branch.bank_branch_id , bank.bank_name, bank_branch.branch_name "
        . " FROM tbl_pet, bank, bank_branch,tbl_pay "
        . " WHERE tbl_pet.status in( 'จัดทำเช็ค', 'เช็คอนุมัติ', 'จ่ายเช็คแล้ว') "
        . " AND tbl_pet.pet_id = tbl_pay.pet_id "
        . " AND tbl_pay.bank_id = bank.bank_id "
        . " AND tbl_pay.bank_branch_id = bank_branch.bank_branch_id ";


//echo $sql;
$result = $db->runSQL($sql);

$tbl_header = '
<table cellspacing="0" cellpadding="0" border="0" width="100%">

';

$line = 0;
while ($rows = $db->fetch_object($result)) {
    $line++;
    $tbl_body = $tbl_body . '
        <tr nobr="true">
            <td align="left" colspan="9" border="0" ><b>' . $rows->bank_name . '   ' . $rows->branch_name . '</b></td>
        </tr>

        ';

    $sql = "SELECT DISTINCT tbl_pay.cheque_no  "
            . " FROM tbl_creditor, bank, bank_branch,tbl_pay, panngan3, tbl_pet"
            . " LEFT JOIN expenses_lvl2 ON ex2_id = expenses_lvl2.expenses2_id "
            . " LEFT JOIN expenses_lvl3 ON ex3_id = expenses_lvl3.expenses3_id "
            . " WHERE tbl_pet.status in( 'จัดทำเช็ค', 'เช็คอนุมัติ', 'จ่ายเช็คแล้ว') "
            . " AND bank.bank_id = '$rows->bank_id' "
            . " AND bank_branch.bank_branch_id = '$rows->bank_branch_id' "
            . " AND tbl_pet.receiver = tbl_creditor.creditor_id "
            . " AND panngan3.panngan3_id = tbl_pet.panngan3_id "
            . " AND tbl_pet.pet_id = tbl_pay.pet_id "
            . " AND tbl_pay.bank_id = bank.bank_id "
            . " AND tbl_pay.bank_branch_id = bank_branch.bank_branch_id "
            . " ORDER BY tbl_pay.cheque_no";

    $result1 = $db->runSQL($sql);
//    $tbl_body = $tbl_body . '
//            <tr align="center" nobr="true" style="border-bottom: 1px dotted black">
//                <td width="10%">เลขที่เช็ค</td>
//                <td width="10%">เลขที่ฎีกา</td>
//                <td width="10%">ผู้รับเงิน</td>
//                <td width="15%">งาน</td>
//                <td width="5%">หมวด</td>
//                <td width="5%">ประเภท</td>
//                <td width="15%" border="0">หน้าฎีกา</td>
//                <td width="20%" border="0">ตามเช็ค</td>
//                <td width="10%">ลายมือชื่อผู้รับเงิน</td>
//            </tr>
//            ';

    

    
    //$x = 0;
    while ($rows1 = $db->fetch_object($result1)) {

        //$x += $rows1->amount;
        $sql = "SELECT DISTINCT tbl_pay.cheque_no , tbl_pet.pet_id , credit_name AS receiver "
                . " , tbl_pay.amount, tbl_pet.net, panngan3.panngan3_id, ex2_id, ex3_id "
                . " FROM tbl_creditor, bank, bank_branch,tbl_pay, panngan3, tbl_pet"
                . " LEFT JOIN expenses_lvl2 ON ex2_id = expenses_lvl2.expenses2_id "
                . " LEFT JOIN expenses_lvl3 ON ex3_id = expenses_lvl3.expenses3_id "
                . " WHERE tbl_pet.status in( 'จัดทำเช็ค', 'เช็คอนุมัติ', 'จ่ายเช็คแล้ว') "
                . " AND bank.bank_id = '$rows->bank_id' "
                . " AND bank_branch.bank_branch_id = '$rows->bank_branch_id' "
                . " AND tbl_pay.cheque_no = '$rows1->cheque_no' "
                . " AND tbl_pet.receiver = tbl_creditor.creditor_id "
                . " AND panngan3.panngan3_id = tbl_pet.panngan3_id "
                . " AND tbl_pet.pet_id = tbl_pay.pet_id "
                . " AND tbl_pay.bank_id = bank.bank_id "
                . " AND tbl_pay.bank_branch_id = bank_branch.bank_branch_id "
                . " ORDER BY tbl_pay.cheque_no";

        $result2 = $db->runSQL($sql);
        
        $numrows = $db->num_rows($result2);
        $i = 1;
        while ($rows2 = $db->fetch_object($result2)) {
            $tbl_body = $tbl_body . '
                    <tr nobr="true">
                        <td width="10%" align="center">' . $rows2->cheque_no . '</td>
                        <td width="7%" align="center">' . $rows2->pet_id . '</td>
                        <td width="20%" align="center">' . $rows2->receiver . '</td>
                        <td width="10%" align="center">' . $rows2->panngan3_id . '</td>
                        <td width="5%" align="center">' . $rows2->ex2_id . '</td>
                        <td width="5%" align="center">' . $rows2->ex3_id . '</td>
                        <td width="15%" align="right" border="0">' . number_format($rows2->net, 2) . '</td>
                            ';

            if ($i == $numrows) {
                $tbl_body .= '<td width="15%" align="right" border="0"><u>' . number_format($rows2->amount, 2) . '</u></td>';
            } else {
                $tbl_body .= '<td width="15%" align="right" border="0">&nbsp;</td>';
            }
            $tbl_body .= '
                        
                        <td width="13%" align="right">&nbsp;</td>
                    </tr>
                    
                    ';
            $i++;
        }

        
        $line++;
    }
    $line++;
    $tbl_body = $tbl_body . '
    <tr height="1" nobr="true"><td colspan="9"><hr></td></tr>
    ';

//    if ($line == 20) {
//        
//        $tbl_footer = '</table>';
//
//        $pdf->writeHTML($tbl_header . $tbl_body . $tbl_footer, true, false, false, false, '');
//        $tbl_body="";
//        $pdf->lastPage();
//        $pdf->AddPage();
//    }
//    $line++;
}

$tbl_footer = '</table>';

//$pdf->writeH
$pdf->writeHTML($tbl_header . $tbl_body . $tbl_footer, true, false, false, false, '');
//$pdf->writeHTML($sql);
//$pdf->Output();
//$pdf->writeHTML($tbl_header . $tbl . $tbl_sum, true, false, false, false, '');
$pdf->Output("pdf", 'I');
?>